package com.css.dzj.zfjg.modules.supervisionreportinfo.service.impl;

import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.QJgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.QJgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.JgTsMsg;
import com.css.dzj.zfjg.modules.supervisionreportinfo.mapper.JgFbJgjbMapper;
import com.css.dzj.zfjg.modules.supervisionreportinfo.mapper.JgTsjbReportInfoMapper;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgFbReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportMsg;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class JgFbReportServiceImpl implements JgFbReportService {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private JgFbJgjbMapper jgFbJgjbMapper;
    @Autowired
    private JgTsjbReportInfoMapper jgTsjbReportInfoMapper;
    /**
     * 分页查询监管举报列表
     *
     */
    @Override
    public QueryResults<ReportMsg> getReportEntity(Pageable pageable, String tipTerritory, String tipObject, String fbBureau,String fbDepartment,
                                                     String tipType, String name, String taskState, String receiveStartTime,
                                                     String receiveEndTime) {
        //监管举报信息表
        QJgTsjbReportInfoEntity qEntity  = QJgTsjbReportInfoEntity.jgTsjbReportInfoEntity;
        //投诉举报用户信息表
        QJgTsjbUserInfoEntity dEntity = QJgTsjbUserInfoEntity.jgTsjbUserInfoEntity;
        //举报任务分办表
        QJgFbJgjbEntity bEntity = QJgFbJgjbEntity.jgFbJgjbEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ("".equals(tipTerritory) || tipTerritory == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipTerritory.like("%"+tipTerritory+"%"));
        predicate = ("".equals(tipObject) || tipObject == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipObject.like("%"+tipObject+"%"));
        predicate = ("".equals(tipType) || tipType == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipType.like("%"+tipType+"%"));
        predicate = ("".equals(taskState) || taskState == null) ? predicate : ExpressionUtils.and(predicate, qEntity.taskState.eq(taskState));
        predicate = ("".equals(name) || name == null) ? predicate : ExpressionUtils.and(predicate, dEntity.name.like("%"+name+"%"));
        predicate = ("".equals(fbBureau) || fbBureau == null) ? predicate : ExpressionUtils.and(predicate, bEntity.fbBureau.like("%"+fbBureau+"%"));
        predicate = ("".equals(fbDepartment) || fbDepartment == null) ? predicate : ExpressionUtils.and(predicate, bEntity.fbDepartment.like("%"+fbDepartment+"%"));
        //任务接受时间
        if((StringUtils.isEmpty(receiveStartTime)) && (!StringUtils.isEmpty(receiveEndTime))) {
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                endDate = sf.parse(receiveEndTime +" 23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.loe(endDate));
        }
        if ((!StringUtils.isEmpty(receiveStartTime)) && (StringUtils.isEmpty(receiveEndTime))) {
            Date startDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(receiveStartTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.goe(startDate));
        }
        if ((!StringUtils.isEmpty(receiveStartTime)) && (!StringUtils.isEmpty(receiveEndTime))) {
            Date startDate = null;
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(receiveStartTime);
                endDate = sf.parse(receiveEndTime + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.receiveTime.between(startDate, endDate));
        }

        return jpaQueryFactory
                .select(
                        Projections.bean(ReportMsg.class,
                                qEntity.registerCode,qEntity.tipTerritory,qEntity.tipObject,bEntity.fbBureau,bEntity.fbDepartment,qEntity.tipType,dEntity.name,qEntity.receiveTime,qEntity.taskState))
                .from(qEntity)
                .leftJoin(dEntity).on(qEntity.registerCode.eq(dEntity.registerCode))
                .leftJoin(bEntity).on(qEntity.registerCode.eq(bEntity.registerCode))
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }
    /**
     * 通过registercode查询监管举报信息
     */
    @Override
    public JgTsjbReportInfoEntity findReportInfo(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgTsjbReportInfoEntity qEntity  = QJgTsjbReportInfoEntity.jgTsjbReportInfoEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }
    /**
     * 通过registercode查询监管举报任务分办表
     */
    @Override
    public JgFbJgjbEntity findRegister(String registerCode) {
        if(StringUtils.isEmpty(registerCode)){
            throw new RuntimeException("参数为空");
        }
        QJgFbJgjbEntity qEntity  = QJgFbJgjbEntity.jgFbJgjbEntity;
        return jpaQueryFactory
                .selectFrom(qEntity)
                .where(  qEntity.registerCode.eq(registerCode)
                )
                .fetchOne();
    }
    /**
     * 添加监管举报分办信息
     * @param jgFbJgjbEntity
     */
    @Transactional
    @Override
    public void add(JgFbJgjbEntity jgFbJgjbEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbJgjbEntity.setCreateUser(userInfo.getLoginName());
            }
            jgFbJgjbEntity.setFbState("101");
            //当查收时分办表添加认领时间（接收时间）
            jgFbJgjbEntity.setReceiveTime(new Date());
            jgFbJgjbEntity.setCreateTime(new Date());
            jgFbJgjbMapper.save(jgFbJgjbEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 修改监管举报分办信息
     * @param jgFbJgjbEntity
     */
    @Transactional
    @Override
    public void fbupdate(JgFbJgjbEntity jgFbJgjbEntity, HttpServletRequest request) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbJgjbEntity.setUpdateUser(userInfo.getLoginName());
            }
            jgFbJgjbEntity.setUpdateTime(new Date());
            jgFbJgjbMapper.save(jgFbJgjbEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 修改监管举报信息
     * @param jgTsjbReportInfoEntity
     */
    @Transactional
    @Override
    public void update(JgTsjbReportInfoEntity jgTsjbReportInfoEntity, HttpServletRequest request) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgTsjbReportInfoEntity.setUpdateUser(userInfo.getLoginName());
            }
            //添加分办时间
            if(StringUtils.isEmpty(jgTsjbReportInfoEntity.getFbTime())){
                jgTsjbReportInfoEntity.setFbTime(new Date());
            }
            jgTsjbReportInfoEntity.setUpdateTime(new Date());
            jgTsjbReportInfoEntity.setTaskState("101");
            jgTsjbReportInfoMapper.save(jgTsjbReportInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 修改监管举报主表信息
     * @param jgTsjbReportInfoEntity
     */
    @Transactional
    @Override
    public void updatejb(JgTsjbReportInfoEntity jgTsjbReportInfoEntity, HttpServletRequest request,String flag) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgTsjbReportInfoEntity.setUpdateUser(userInfo.getLoginName());
            }
            if(StringUtils.isEmpty(jgTsjbReportInfoEntity.getFbTime())){
                jgTsjbReportInfoEntity.setFbTime(new Date());
            }
            if(flag.equals("105")){
                jgTsjbReportInfoEntity.setFkTime(new Date());
            }else if(flag.equals("106")){
                jgTsjbReportInfoEntity.setEndTime(new Date());
            }
            jgTsjbReportInfoEntity.setUpdateTime(new Date());
            jgTsjbReportInfoEntity.setTaskState(flag);
            jgTsjbReportInfoMapper.save(jgTsjbReportInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
